#!/bin/sh

. $LKP_SRC/lib/tests/cassandra.sh
. $LKP_SRC/lib/reproduce-log.sh
. $LKP_SRC/lib/env.sh

service mysqld start

check_mysql_server()
{
	retries=10
	for i in $(seq $retries);
	do
		mysqladmin ping | grep alive
		return_value=$?
		if [ "$return_value" -eq 1 ]; then
			sleep 10
		else
			return
		fi
	done
	die "mysql server is not available."
}

check_mysql_server
setup_java_home

cd "$BENCHMARK_ROOT/linkbench" || exit

cp config/LinkConfigMysql.properties config/MyConfig.properties

mysql << "MY_QUERY"
SET GLOBAL max_connections = 1025;
DROP DATABASE IF EXISTS linkdb;
CREATE DATABASE linkdb;
USE linkdb;

CREATE TABLE `linktable` (
	`id1` bigint(20) unsigned NOT NULL DEFAULT '0',
	`id2` bigint(20) unsigned NOT NULL DEFAULT '0',
	`link_type` bigint(20) unsigned NOT NULL DEFAULT '0',
	`visibility` tinyint(3) NOT NULL DEFAULT '0',
	`data` varchar(255) NOT NULL DEFAULT '',
	`time` bigint(20) unsigned NOT NULL DEFAULT '0',
	`version` int(11) unsigned NOT NULL DEFAULT '0',
	PRIMARY KEY (link_type, `id1`,`id2`),
	KEY `id1_type` (`id1`,`link_type`,`visibility`,`time`,`id2`,`version`,`data`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 PARTITION BY key(id1) PARTITIONS 16;

CREATE TABLE `counttable` (
	`id` bigint(20) unsigned NOT NULL DEFAULT '0',
	`link_type` bigint(20) unsigned NOT NULL DEFAULT '0',
	`count` int(10) unsigned NOT NULL DEFAULT '0',
	`time` bigint(20) unsigned NOT NULL DEFAULT '0',
	`version` bigint(20) unsigned NOT NULL DEFAULT '0',
	PRIMARY KEY (`id`,`link_type`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `nodetable` (
	`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
	`type` int(10) unsigned NOT NULL,
	`version` bigint(20) unsigned NOT NULL,
	`time` int(10) unsigned NOT NULL,
	`data` mediumtext NOT NULL,
	PRIMARY KEY(`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE USER 'linkbench'@'localhost' IDENTIFIED BY 'mypassword';
GRANT ALL PRIVILEGES ON linkdb.* TO 'linkbench'@'localhost' WITH GRANT OPTION;
MY_QUERY

mysql << "MY_QUERY"
USE linkdb;
ALTER TABLE linktable DROP KEY `id1_type`;
SET GLOBAL innodb_flush_log_at_trx_commit = 2;
SET GLOBAL sync_binlog = 0;
MY_QUERY

./bin/linkbench 2>&1 -c config/MyConfig.properties \
-D host=localhost -D user=linkbench -D password=mypassword  -l

mysql << "MY_QUERY"
USE linkdb;
SET GLOBAL innodb_flush_log_at_trx_commit = 1;
SET GLOBAL sync_binlog = 1;
ALTER TABLE linktable ADD KEY `id1_type`
	(`id1`,`link_type`,`visibility`,`time`,`id2`,`version`,`data`);
MY_QUERY
